<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="产品类型" prop="productTypeCode">
          <el-select
            v-model="queryParams.productTypeCode"
            placeholder="产品类型"
            clearable
            filterable
            style="width: 240px"
           >
            <el-option
              v-for="item in productTypeList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品代码" prop="productCode">
          <el-input
            v-model="queryParams.productCode"
            placeholder="请输入产品代码"
            clearable
            filterable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品缩写" prop="productAbbr">
          <el-input
            v-model="queryParams.productAbbr"
            placeholder="请输入产品缩写"
            clearable
            filterable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="cpu型号" prop="cpuModel">
          <el-input
            v-model="queryParams.cpuModel"
            placeholder="请输入cpu型号"
            clearable
            filterable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['produce:product-info:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddDefault"
            v-hasPermi="['produce:product-info:add']"
          >新增（能力集与测试项默认为选中记录的）</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['produce:product-info:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['produce:product-info:removed']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['produce:product-info:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="ProductInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="产品代码" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <router-link :to="'/produce/product-info/detail/index/' + scope.row.productCode +'&'+ scope.row.productName + '&' + scope.row.productTypeCode + '&' + scope.row.productTypeName" class="link-type">
                <el-popover placement="top" width="900" trigger="hover" >
                  <div style="text-align: center;line-height: 15px;height: 15px;font-size: 25px;">产品详情</div>
                  <div style="height: 15px;line-height: 15px;margin-top: 20px;border-bottom:1px solid black">
                    <div style="float: left;width: 10%;text-align: center;">排序</div>
                    <div style="float: left;width: 20%">产品类型明细代码</div>
                    <div style="float: left;width: 25%">产品类型明细名称</div>
                    <div style="float: left;width: 25%">值 </div>
                    <div style="float: left;width: 20%">备注</div>
                  </div>

                  <div v-for="(item,index) in scope.row.details" :key="index" style="width: 100%;margin-top:5px;border-bottom:1px solid black;	position:relative;height:auto">
                    <div style="width: 100%;height:auto">
                      <div style="float: left;width: 10%;text-align: center;height:100%">{{ item.sort }}</div>
                      <div style="float: left;width: 20%;height:100%">{{ item.typeDetailCode }}</div>
                      <div style="float: left;width: 25%;height:100%">{{ item.typeDetailName }}</div>
                      <div style="float: left;width: 25%;height:100%">{{ item.typeDetailValue }}</div>
                      <div style="float: left;width: 20%;height:100%">{{ item.remark }}</div>
                    </div>

                  </div>
                  <span slot="reference">{{ scope.row.productCode }}</span>
                </el-popover>
                  
              </router-link>
          </template>
        </el-table-column>
        <el-table-column label="产品缩写" align="center" prop="productAbbr" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="产品类型代码" align="center" prop="productTypeCode" />
        <el-table-column label="产品类型名称" align="center" prop="productTypeName" />
        <el-table-column label="cpu型号" align="center" prop="cpuModel" />
        <el-table-column label="锁类型" align="center" prop="lockType">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.lockType === 0"
              type="success"
              :style="{ 'padding-left': '5px', 'padding-right': '5px' }"
            >通用方伯弟锁</el-tag>
            <el-tag v-if="scope.row.lockType === 1"
              type="warning"
              :style="{ 'padding-left': '5px', 'padding-right': '5px' }"
            >享取定制锁</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="oem" align="center" prop="oem" />
        <el-table-column label="是否为子型号" align="center" prop="isSub">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.isSub === 1?'success' : 'danger'"
              :style="{ 'padding-left': '5px', 'padding-right': '5px' }"
            >{{ scope.row.isSub === 1 ? '是' : '否' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="父型号代码" align="center" prop="parentCode"/>
        <el-table-column label="产品能力绑定信息" align="center" prop="productInfoPowerBindVos" width="200">
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.productInfoPowerBindVos" :key="index" >
              <span>{{ item.powerName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="产品测试项绑定信息" align="center" prop="productClientTestBindVos" width="200"> 
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.productClientTestBindVos" :key="index" >
              <span>{{index +'.'+ item.testItemName }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdatePower(scope.row)"
                  v-show="scope.row.productInfoPowerBindVos"
                >修改能力集</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelPower(scope.row)"
                  v-show="scope.row.productInfoPowerBindVos"
                >删除能力集</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-plus"
                  @click="handleBindingPower(scope.row)"
                  v-if="!scope.row.productInfoPowerBindVos"
                >绑定能力集</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-plus"
                  @click="handleBindingTestItem(scope.row)"
                  v-if="!scope.row.productClientTestBindVos"
                >绑定测试项</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdateTestItem(scope.row)"
                  v-show="scope.row.productClientTestBindVos"
                >修改测试项</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelTestItem(scope.row)"
                  v-show="scope.row.productClientTestBindVos"
                >删除测试项</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['produce:product-info:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['produce:product-info:removed']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改产品信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="110px">
          <el-form-item label="为子型号" prop="isSub">
            <!-- <el-input v-model="form.productTypeCode" placeholder="请输入产品类型代码" /> -->
              <el-select
              v-model="form.isSub"
              placeholder="请选择"
              clearable
              filterable
            >
              <el-option
                v-for="item in isSub_list"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="父型号产品类型" prop="parentProductTypeCode" v-show="form.isSub === 1">
            <el-select
              v-model="form.parentProductTypeCode"
              placeholder="父型号产品类型"
              clearable
              filterable
              style="width: 240px"
              @change="handleParentProductTypeChange"
            >
              <el-option
                v-for="item in productTypeList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="父型号产品代码" prop="parentCode" v-show="form.isSub === 1">
            <!-- <el-input v-model="form.productTypeCode" placeholder="请输入产品类型代码" /> -->
              <el-select
              v-model="form.parentCode"
              placeholder="请选择"
              clearable
              filterable
              style="width: 240px"
            >
              <el-option
                v-for="item in parentProductTypeCode_list"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产品代码" prop="productCode">
            <el-input v-model="form.productCode" placeholder="请输入产品代码" :disabled="title == '修改产品信息'" type="text" maxlength="25" show-word-limit/>
          </el-form-item>
          <el-row>
            <el-col :span="16">
              <el-form-item label="产品缩写" prop="productAbbr">
                <el-input v-model="form.productAbbr" placeholder="请输入缩写" type="text" maxlength="2" show-word-limit/>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="form.productName" placeholder="请输入产品名称" type="text" maxlength="30" show-word-limit/>
          </el-form-item>
          <el-form-item label="产品类型" prop="productTypeCode">
            <!-- <el-input v-model="form.productTypeCode" placeholder="请输入产品类型代码" /> -->
              <el-select
                v-model="form.productTypeCode"
                placeholder="请选择产品类型"
                clearable
                filterable
                style="width: 240px"
              >
                <el-option
                  v-for="item in productTypeList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
          </el-form-item>
          <el-form-item label="锁类型" prop="lockType">
            <!-- <el-input v-model="form.productTypeCode" placeholder="请输入产品类型代码" /> -->
              <el-select
                v-model="form.lockType"
                placeholder="请选择锁类型"
                clearable
                filterable
                style="width: 240px"
              >
                <el-option
                    v-for="dict in dict.type.lock_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
          </el-form-item>


          <el-form-item label="cpu型号" prop="cpuModel">
            <el-input v-model="form.cpuModel" placeholder="请输入cpu型号" type="text" maxlength="10" show-word-limit/>
          </el-form-item>
          <el-form-item label="oem" prop="oem">
            <el-input v-model="form.oem" placeholder="请输入oem" type="number" show-word-limit/>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

       <!-- 绑定或修改产品绑定的能力集 -->
       <el-dialog :title="this.bingdingPowerTitle" :visible.sync="bingdingPowerDialog" width="500px" append-to-body>
        <el-form ref="powerForm" :model="powerForm" :rules="powerRules" label-width="110px">
          <el-form-item label="产品型号代码" prop="productCode">
            <el-input v-model="powerForm.productCode" placeholder="请输入产品型号代码" disabled/>
          </el-form-item>
          <el-form-item label="能力代码" prop="powerCode">
            <el-select
                v-model="powerForm.powerCode"
                placeholder="请选择产品类型"
                clearable
                filterable
                multiple
                style="width: 240px"
                @change="handelChooseChange"
              >
                <el-option
                  v-for="item in this.productInfoPowerList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitPowerForm">确 定</el-button>
          <el-button @click="cancelPowerForm">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 绑定或修改产品绑定的测试项 -->
      <el-dialog :title="this.bingdingTestItemTitle" :visible.sync="bingdingTestItemDialog" width="500px" append-to-body>
        <el-form ref="testItemForm" :model="testItemForm" :rules="testItemRules" label-width="110px">
          <el-form-item label="产品型号代码" prop="productCode">
            <el-input v-model="testItemForm.productCode" placeholder="请输入产品型号代码" disabled/>
          </el-form-item>
          <div v-for="(item, index) in testItemForm.testItems" :key="index" style="background-color:#eEeeee;margin-top:5px;padding:10px 20px;border-radius:5px">
              <div v-if="index > 0" style="margin-top:5px" @click="delTestItem(item, index)">
                  <span class="el-icon-remove"></span>
              </div>
              <div v-else style="margin-top:20px"></div>
              <!-- <el-form-item :label="`排序`" :prop="testItemForm.testItems[index].sort">
                  <el-input v-model="item.sort" type="text" placeholder="请自定义排序" @input="handelChooseChange"/>
              </el-form-item> -->
              <el-form-item :label="`测试项-`+(index+1)" :prop="testItemForm.testItems[index].testItemCode">
                  <!-- <el-input v-model="item.testItemCode" placeholder="请输入相对路径"/> -->
                  <el-select
                    v-model="item.testItemCode"
                    placeholder="请选择测试项"
                    clearable
                    filterable
                    style="width: 310px"
                    @change="handelChooseChange"
                  >
                    <el-option
                      v-for="item in testItem_list"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
              </el-form-item>
          </div>
          <div class="addBtn" @click="addTestItem">
              <el-button size="mini" type="text" icon="el-icon-edit">
                  新增
              </el-button>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitTestItemForm">确 定</el-button>
          <el-button @click="cancelTestItemForm">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listProductInfo, getProductInfo, removedProductInfo, addProductInfo, updateProductInfo } from "@/api/produce/product-info";
  import { listProductType } from "@/api/produce/product-type";
  import { productClientTestBatchAddBind, productClientTestBatchUpdateBind, getProductTestItemByProductCode, delProductClientTestBind } from "@/api/produce/product-client-test-bind";
  import { productInfoPowerBatchAddBind, productInfoPowerBatchUpdateBind, getProductPowerByProductCode, delProductPowerBind } from "@/api/produce/product-info-power-bind";
  import { listProductInfoPower } from "@/api/produce/product-info-power";
  import { listProductClientTestItem } from "@/api/produce/product-client-test-item";
  export default {
    name: "Product-info",
    dicts:['lock_type'],
    data() {
      return {
        // 选中记录的数量
        selectionNum:0,
        // 选中记录的测试项
        defaultClientTestBindVos:null,
        // 选中记录的能力集
        defaultPowerBindVos:null,
        // 使用选中记录的能力集和测试项标记
        useDefaultOptions:false,
        // 排序序号
        sortNumber:null,
        // 测试项绑定弹出层标题
        bingdingTestItemTitle:'',
        // 测试项弹窗
        bingdingTestItemDialog: false,
        // 测试项集合
        testItem_list:[],
        // 测试项表单
        testItemForm:{},
        // 能力集表单
        powerForm:{},
        // 能力集绑定弹出层标题
        bingdingPowerTitle:'',
        // 能力集弹窗
        bingdingPowerDialog: false,
        // 能力集集合
        productInfoPowerList:[],
        // 父型号产品代码列表
        parentProductTypeCode_list:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 是否为子型号
        isSub_list: [
          { value: 0, label: "否" },
          { value: 1, label: "是" }
        ],
        // 产品信息表格数据
        ProductInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          productCode: null,
          productAbbr: null,
          productName: null,
          productTypeCode: null,
          cpuModel: null,
          removed: null,
          orderByColumn: 'createTime',
          isAsc: 'desc'
        },
        // 产品类型数据
        productTypeList: [],
        productTypeQueryParams:{
          pageNum: 1,
          pageSize: 1000,
        },
        // 表单参数
        form: {},
        // 表单校验
        testItemRules:{
          testItems: [
            { required: true, message: "请至少选择一种测试项", trigger: "blur" },
          ],
        },
        powerRules:{
          powerCode: [
            { required: true, message: "请至少选择一种能力", trigger: "blur" },
          ],
        },
        rules: {
          isSub: [
            { required: true, message: "请选择是否为子型号", trigger: "blur" },
            // { required: true, message: "输入规则：数字、大小写字母、'-'号", trigger: "blur", pattern: /^[a-zA-Z0-9-]+$/ } //大小写字母数字-
          ],
          productCode: [
            { required: true, message: "产品代码不能为空", trigger: "blur" },
            // { required: true, message: "输入规则：数字、大小写字母、'-'号", trigger: "blur", pattern: /^[a-zA-Z0-9-]+$/ } //大小写字母数字-
          ],
          productAbbr: [
            { required: true, message: "产品缩写不能为空", trigger: "blur" },
            { required: true, message: "输入规则：数字、大小写字母", trigger: "blur", pattern: /^[a-zA-Z0-9]+$/ } //大小写字母数字
          ],
          productName: [
            { required: true, message: "产品名称不能为空", trigger: "blur" }
          ],
          productTypeCode: [
            { required: true, message: "产品类型代码不能为空", trigger: "blur" }
          ],
          cpuModel: [
            { required: true, message: "cpu型号不能为空", trigger: "blur" },
            { required: true, message: "输入规则：数字、大小写字母", trigger: "blur", pattern: /^[a-zA-Z0-9]+$/ } //大小写字母数字
          ],
        }
      };
    },
    created() {
      this.getProductType();
    },
    methods: {
      // 删除产品的测试项
      handleDelTestItem(row){
        // 删除交互
        this.$modal.confirm('确认删除产品:"' + row.productName + '"的测试项绑定关系？').then(function() {
        }).then(() => {
          let ids = ''
          for(var i=0;i<row.productClientTestBindVos.length;i++){
            if(ids == ''){
              ids = ids + row.productClientTestBindVos[i].id 
            }else{
              ids = ids + ',' + row.productClientTestBindVos[i].id 
            }
          }
          delProductClientTestBind(ids).then((res) =>{
            if(res.code === 200){
              this.$message.success("产品测试项绑定关系删除成功")
              this.getList()
            }else{
              this.$message.warning("产品测试项绑定关系删除失败,原因："+res.msg)
            }
          }).catch((err) =>{
            this.$message.error("产品测试项绑定关系删除失败,原因："+err.msg)
          })

        }).catch(() => {});
      },
      // 删除产品的能力集
      handleDelPower(row){
        // 删除交互
        this.$modal.confirm('确认删除产品:"' + row.productName + '"的能力集绑定关系？').then(function() {
        }).then(() => {
          let ids = ''
          for(var i=0;i<row.productInfoPowerBindVos.length;i++){
            ids = ids + row.productInfoPowerBindVos[i].id + ','
          }
          delProductPowerBind(ids).then((res) =>{
            if(res.code === 200){
              this.$message.success("产品能力集绑定关系删除成功")
              this.getList()
            }else{
              this.$message.warning("产品能力集绑定关系删除失败,原因："+res.msg)
            }
          }).catch((err) =>{
            this.$message.error("产品能力集绑定关系删除失败,原因："+err.msg)
          })

        }).catch(() => {});
      },
      // 新增测试项
      addTestItem(){
        this.sortNumber ++ 
        this.testItemForm.testItems.push({
          productCode:this.testItemForm.testItems[0].productCode,
          sort:this.sortNumber ,
          testItemCode:null
        });
        this.$forceUpdate();
      },
      // 删除表单中的测试项
      delTestItem(item, index) {
        if (this.testItemForm.testItems.length > 1) {
            // 删除
            this.testItemForm.testItems.splice(index, 1);
            // 后面的进位
            for(var i = index ;i< this.testItemForm.testItems.length; i++){
              this.testItemForm.testItems[i].sort = this.testItemForm.testItems[i].sort - 1
            }
        } else {
            this.$message.warning('至少选择一项');
        }
        this.$forceUpdate();
      },
      // 刷新
      handelChooseChange(){
        this.$forceUpdate();
      },
      /**查询产品类型下拉 */
      getProductType () {
        listProductType(this.productTypeQueryParams).then(response => {
          this.productTypeList = response.rows;
          // 下拉框赋默认值
          this.queryParams.productTypeCode = this.productTypeList[0].code;
          // 再去查询列表
          this.getList();
        });
      },
      /** 点击父型号产品类型-获取父型号产品代码列表 */
      handleParentProductTypeChange(){
        this.form.productTypeCode = this.form.parentProductTypeCode
        const params={
          pageNum: 1,
          pageSize: 1000,
          productTypeCode: this.form.parentProductTypeCode
        }
        listProductInfo(params).then(response => {
          for(var i=0 ; i<response.rows.length ; i++){
            let temp = {
              value:response.rows[i].productCode,
              label:response.rows[i].productCode
            }
            this.parentProductTypeCode_list.push(temp);
          }
        }).catch(error => {
          this.$message.error("查询父型号产品代码列表失败",error.msg);
        }).finally(() =>{
          this.$forceUpdate();
        })
      },
      /** 查询产品信息列表 */
      getList() {
        this.loading = true;
        listProductInfo(this.queryParams).then(response => {
          // 预处理排序
          for(var i = 0;i<response.rows.length;i++){
            if(response.rows[i].productClientTestBindVos){
              response.rows[i].productClientTestBindVos.sort(function(a,b){
                return a.sort - b.sort
              })
            }
          };
          this.ProductInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消绑定测试项
      cancelTestItemForm(){
        this.bingdingTestItemDialog = false;
      },
      // 取消绑定能力集
      cancelPowerForm() {
        this.bingdingPowerDialog = false;
        this.reset();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          productCode: null,
          productName: null,
          productTypeCode: null,
          cpuModel: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          removed: null
        };
        this.powerForm = {
          id: null,
          productCode: null,
          powerCode:[],
          ProductInfoPowerBindList:[]
        };
        this.testItemForm = {
          id:null,
          productCode:null,
          testItems:[
            {
              productCode:null,
              sort:0,
              testItemCode:null
            }
          ],
          productClientTestBindList:[]
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
        this.selectionNum = selection.length
        if(selection[0].productClientTestBindVos){
          this.defaultClientTestBindVos = selection[0].productClientTestBindVos;
        }
        if(selection[0].productInfoPowerBindVos){
          this.defaultPowerBindVos = selection[0].productInfoPowerBindVos;
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.useDefaultOptions = false;
        this.title = "添加产品信息";
      },
      handleAddDefault(){
        if(this.selectionNum !== 1){
          this.$message.warning("请选择一条产品信息为模板")
          return
        }
        if(this.defaultClientTestBindVos.length === 0 && this.defaultPowerBindVos.length === 0){
          this.$message.warning("您选择的记录测试项或能力集皆未绑定")
          return
        }
        this.$confirm('是否默认绑定选中的产品信息的测试项和能力集？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.reset();
          this.open = true;
          this.title = "添加产品信息(默认测试项和能力集)";
          this.useDefaultOptions = true;
        })
      },
      /** 获取测试项列表 */
      getTestItemList(){
        const params={
          pageNum:1,
          pageSize:1000
        }
        listProductClientTestItem(params).then(response => {
          if(response.code === 200){
            for(var i = 0;i<response.rows.length;i++){
              let temp={
                code:response.rows[i].code,
                name:response.rows[i].name
              }
              this.testItem_list.push(temp);
            }
            this.$forceUpdate();
          }else{
            this.$message.warning("获取测试项失败，原因：",response.msg);
          }
        }).catch((err) =>{
          this.$message.error("listProductClientTestItem获取测试项失败",err.msg);
        })
      },
      /** 获取能力集列表 */
      getPowerList(){
        const params={
          pageNum:1,
          pageSize:1000
        }
        listProductInfoPower(params).then(response => {
          if(response.code === 200){
            for(var i = 0;i<response.rows.length;i++){
              let temp={
                code:response.rows[i].powerCode,
                name:response.rows[i].powerName
              }
              this.productInfoPowerList.push(temp);
            }
            this.$forceUpdate();
          }else{
            this.$message.warning("获取能力集失败，原因：",response.msg);
          }
        }).catch((err) =>{
          this.$message.error("listProductInfoPower获取能力集失败",err.msg);
        })
      },
      /** 修改测试项操作 */
      handleUpdateTestItem(row){
        this.testItemForm.id = row.id;
        this.testItemForm.productCode = row.productCode;
        this.testItemForm.testItems = []
        // 获取测试项集合
        if(this.testItem_list.length === 0){
          this.getTestItemList();
        }
        // 获取产品测试项详情
        const param = {
          productCode : row.productCode
        }
        getProductTestItemByProductCode(param).then(response => {
          if(response.code === 200){
            if(response.data){
              // 获取最大排序数
              let maxMark = 0;
              for(var i=0;i<response.data.length;i++){
                if(response.data[i].sort > maxMark){
                  maxMark = response.data[i].sort;
                }
                let temp={
                  productCode:response.data[i].productCode,
                  sort:response.data[i].sort,
                  testItemCode:response.data[i].testItemCode
                }
                this.testItemForm.testItems.push(temp)
              }
              // 排序起始数
              this.sortNumber = maxMark;
              // 对testItems进行排序
              this.testItemForm.testItems.sort(function(a,b){
                return a.sort - b.sort;
              })
              this.$forceUpdate();
              this.bingdingTestItemDialog = true;
              this.bingdingTestItemTitle = "修改测试项";
            }
          }else{
            this.$message.warning(response.msg);
          }
        });
      },
      /** 修改能力集操作 */
      handleUpdatePower(row) {
        this.powerForm.id = row.id;
        this.powerForm.productCode = row.productCode;
        this.powerForm.powerCode = []
        // 获取能力集列表
        if(this.productInfoPowerList.length === 0){
          this.getPowerList();
        }
        // 获取产品能力集详情
        const param = {
          productCode : row.productCode
        }
        getProductPowerByProductCode(param).then(response => {
          if(response.code === 200){
            if(response.data){
              for(var i=0;i<response.data.length;i++){
                this.powerForm.powerCode.push(response.data[i].powerCode)
              }
              this.$forceUpdate();
              this.bingdingPowerDialog = true;
              this.bingdingPowerTitle = "修改能力集";
            }
          }else{
            this.$message.warning(response.msg);
          }
        });
      },
      /** 绑定能力集操作 */
      handleBindingPower(row) {
        this.reset();
        this.powerForm.productCode = row.productCode;
        // 只在第一次点击绑定能力集时调用接口，避免反复调用
        if(this.productInfoPowerList.length === 0){
          this.getPowerList();
        }
        this.bingdingPowerDialog = true;
        this.bingdingPowerTitle = "绑定能力集";
      },
      /** 绑定测试项操作 */
      handleBindingTestItem(row) {
        this.reset();
        this.sortNumber = 0;
        this.testItemForm.productCode = row.productCode;
        this.testItemForm.testItems=[{productCode:this.testItemForm.productCode,sort:0,testItemCode:null}]
        // 只在第一次点击绑定能力集时调用接口，避免反复调用
        if(this.testItem_list.length === 0){
          this.getTestItemList();
        }
        this.bingdingTestItemDialog = true;
        this.bingdingTestItemTitle = "绑定测试项";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getProductInfo(id).then(response => {
          this.form = response.data;
          this.form.parentProductTypeCode = response.data.productTypeCode
          this.form.lockType = response.data.lockType !== null ? response.data.lockType.toString() : null
          this.open = true;
          this.title = "修改产品信息";
        });
      },
      /** 绑定测试项提交按钮 */
      submitTestItemForm(){
        this.$refs["testItemForm"].validate(valid => {
          if (valid) {
            if (this.testItemForm.id != null) {
              let params = []
              for(var i = 0 ;i < this.testItemForm.testItems.length ;i++){
                let temp = {
                  productCode:this.testItemForm.testItems[i].productCode,
                  sort:i,
                  testItemCode:this.testItemForm.testItems[i].testItemCode
                }
                params.push(temp)
                // this.testItemForm.testItems[i].productCode = this.testItemForm.productCode;
                // this.testItemForm.testItems[i].sort = parseInt(this.testItemForm.testItems[i].sort);
              }
              productClientTestBatchUpdateBind(params).then(response => {
                if(response.code === 200){
                  this.$modal.msgSuccess("修改测试项成功");
                  this.getList()
                }else{
                  this.$modal.msgSuccess("修改测试项失败！原因："+response.msg);
                }
              }).catch((err) =>{
                this.$modal.msgSuccess("修改测试项失败！接口原因："+err);
              }).finally(() =>{
                this.bingdingTestItemDialog = false;
              })
            } else {
              for(var i = 0 ;i < this.testItemForm.testItems.length ;i++){
                this.testItemForm.testItems[i].productCode = this.testItemForm.productCode;
                this.testItemForm.testItems[i].sort = i;
              }
              productClientTestBatchAddBind(this.testItemForm.testItems).then(response => {
                if(response.code === 200){
                  this.$modal.msgSuccess("绑定测试项成功");
                  this.getList()
                }else{
                  this.$modal.msgSuccess("绑定测试项失败！原因："+response.msg);
                }
              }).catch((err) =>{
                this.$modal.msgSuccess("绑定测试项失败！接口原因："+err);
              }).finally(() =>{
                this.bingdingTestItemDialog = false;
              })
            }
          }
        });
      },
      /** 绑定能力集提交按钮 */
      submitPowerForm() {
        this.powerForm.ProductInfoPowerBindList=[]
        this.$refs["powerForm"].validate(valid => {
          if (valid) {
            if (this.powerForm.id != null) {
              for(var i = 0 ;i < this.powerForm.powerCode.length ;i++){
                let temp={
                  productCode:this.powerForm.productCode,
                  powerCode:this.powerForm.powerCode[i]
                }
                this.powerForm.ProductInfoPowerBindList.push(temp)
              }
              productInfoPowerBatchUpdateBind(this.powerForm.ProductInfoPowerBindList).then(response => {
                if(response.code === 200){
                  this.$modal.msgSuccess("修改能力集成功");
                  this.getList()
                }else{
                  this.$modal.msgSuccess("修改能力集失败！原因："+response.msg);
                }
              }).catch((err) =>{
                this.$modal.msgSuccess("修改能力集失败！接口原因："+err);
              }).finally(() =>{
                this.bingdingPowerDialog = false;
              })
            } else {
              // 传参处理
              for(var i = 0 ;i < this.powerForm.powerCode.length ;i++){
                let temp={
                  productCode:this.powerForm.productCode,
                  powerCode:this.powerForm.powerCode[i]
                }
                this.powerForm.ProductInfoPowerBindList.push(temp)
              }
              productInfoPowerBatchAddBind(this.powerForm.ProductInfoPowerBindList).then(response => {
                if(response.code === 200){
                  this.$modal.msgSuccess("绑定能力集成功");
                  this.getList()
                }else{
                  this.$modal.msgSuccess("绑定能力集失败！原因："+response.msg);
                }
              }).catch((err) =>{
                this.$modal.msgSuccess("绑定能力集失败！接口原因："+err);
              }).finally(() =>{
                this.bingdingPowerDialog = false;
              })
            }
          }
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateProductInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProductInfo(this.form).then(response => {
                //产品信息新增成功
                this.$modal.msgSuccess("产品信息新增成功");
                this.open = false;
                //绑定默认的测试项和能力集
                if(this.useDefaultOptions){
                  if(this.defaultClientTestBindVos){
                    let params = []
                    for(var i = 0 ;i < this.defaultClientTestBindVos.length ;i++){
                      let temp={
                        productCode:this.form.productCode,
                        testItemCode:this.defaultClientTestBindVos[i].testItemCode,
                        sort:i
                      }
                      params.push(temp)
                    }
                    productClientTestBatchAddBind(params).then(response => {
                      if(response.code === 200){
                        this.$modal.msgSuccess("默认测试项绑定成功");
                        this.getList();
                      }else{
                        this.$modal.msgSuccess("默认测试项绑定失败,原因："+response.msg)
                      }
                    }).catch((err) =>{
                      this.$message.error("默认测试项绑定失败,接口原因："+err)
                    })
                  }
                  if(this.defaultPowerBindVos){
                    let params = []
                    for(var i = 0 ;i < this.defaultPowerBindVos.length ;i++){
                      let temp={
                        productCode:this.form.productCode,
                        powerCode:this.defaultPowerBindVos[i].powerCode
                      }
                      params.push(temp)
                    }
                    productInfoPowerBatchAddBind(params).then(response => {
                      if(response.code === 200){
                        this.$modal.msgSuccess("默认能力集绑定成功");
                        this.getList();
                      }else{
                        this.$modal.msgSuccess("默认能力集绑定失败,原因："+response.msg)
                      }
                    }).catch((err) =>{
                      this.$message.error("默认能力集绑定失败,接口原因："+err)
                    })
                  }
                }else{
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }

              }).catch((err) =>{
                this.$message.error("新增产品信息失败,原因："+err.msg)
              })
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除产品信息编号为"' + ids + '"的数据项？').then(function() {
            // 修改为逻辑删除
          return removedProductInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('produce/product-info/export', {
          ...this.queryParams
        }, `product-info_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  